<?php


namespace app\home\controller;

use app\home\model\Shiyanshi as ShiyanshiModel;
use think\Controller;
use think\Request;

class Shiyanshi extends Controller
{
    protected $shiyanshiModel;

    public function __construct(
        ShiyanshiModel $shiyanshiModel
    )
    {
        parent::__construct();
        $this->shiyanshiModel = $shiyanshiModel;
    }

    public function show(Request $request)
    {
        return $this->fetch();
    }

    public function lists()
    {
        return $this->fetch();
    }

    public function ajax_lists(Request $request)
    {
        $page = $request->param('page', 1);
        $limit = $request->param('limit', 10);
        $where = [];
        $data = $this->shiyanshiModel->where($where)->page($page, $limit)->select();
        $count = $this->shiyanshiModel->where($where)->count();
        return reorganizeTableData(0, '', $count, $data);
    }

    public function info(Request $request)
    {
        $pid = $request->param('pid', '');
        $res = $this->shiyanshiModel->where('pid', $pid)->find();
        return jsonRes($res);
    }

    public function update(Request $request)
    {
        $pid = $request->param('pid', '');
        if(!isset($pid) || empty($pid)){
            return jsonRes([],'pid不能为空',99);
        }
        $xsh_zongdanbai = $request->param('xsh_zongdanbai', '');
        $xsh_baidanbai = $request->param('xsh_baidanbai', '');
        $xsh_q_baidanbai = $request->param('xsh_q_baidanbai', '');
        $xsh_q_baidanbai_dw = $request->param('xsh_q_baidanbai_dw', 0);
        $xsh_zhuantiedanbai = $request->param('xsh_zhuantiedanbai', '');
        $xsh_fanyingdanbai = $request->param('xsh_fanyingdanbai', '');
        $xsh_xuetang = $request->param('xsh_xuetang', '');
        $xsh_ast = $request->param('xsh_ast', '');
        $xsh_alt = $request->param('xsh_alt', '');
        $xsh_baijiesu_1 = $request->param('xsh_baijiesu_1', '');
        $xsh_tnf_a = $request->param('xsh_tnf_a', '');
        $xsh_jigan = $request->param('xsh_jigan', '');
        $xsh_niaosudan = $request->param('xsh_niaosudan', '');
        $xsh_zongdanhongsu = $request->param('xsh_zongdanhongsu', '');
        $xsh_zhijiedanhongsu = $request->param('xsh_zhijiedanhongsu', '');
        $xsh_zongdanguchun = $request->param('xsh_zongdanguchun', '');
        $xsh_ganyousanzhi = $request->param('xsh_ganyousanzhi', '');
        $xsh_gmdzdbdgc = $request->param('xsh_gmdzdbdgc', '');
        $xsh_dmdzdbdgc = $request->param('xsh_dmdzdbdgc', '');
        $xsh_baijiesu_6 = $request->param('xsh_baijiesu_6', '');
        $xcg_xuehongdanbai = $request->param('xcg_xuehongdanbai', '');
        $xcg_baixibao = $request->param('xcg_baixibao', '');
        $xcg_xibaojishu = $request->param('xcg_xibaojishu', '');
        $xcg_linba_xibaojishu = $request->param('xcg_linba_xibaojishu', '');
        $xcg_hongxibao = $request->param('xcg_hongxibao', '');
        $xcg_xuexiaoban = $request->param('xcg_xuexiaoban', '');

        $data = [
            'pid' => $pid,
            'xsh_zongdanbai' => $xsh_zongdanbai,
            'xsh_baidanbai' => $xsh_baidanbai,
            'xsh_q_baidanbai' => $xsh_q_baidanbai,
            'xsh_q_baidanbai_dw' => $xsh_q_baidanbai_dw,
            'xsh_zhuantiedanbai' => $xsh_zhuantiedanbai,
            'xsh_fanyingdanbai' => $xsh_fanyingdanbai,
            'xsh_xuetang' => $xsh_xuetang,
            'xsh_ast' => $xsh_ast,
            'xsh_alt' => $xsh_alt,
            'xsh_baijiesu_1' => $xsh_baijiesu_1,
            'xsh_tnf_a' => $xsh_tnf_a,
            'xsh_jigan' => $xsh_jigan,
            'xsh_niaosudan' => $xsh_niaosudan,
            'xsh_zongdanhongsu' => $xsh_zongdanhongsu,
            'xsh_zhijiedanhongsu' => $xsh_zhijiedanhongsu,
            'xsh_zongdanguchun' => $xsh_zongdanguchun,
            'xsh_ganyousanzhi' => $xsh_ganyousanzhi,
            'xsh_gmdzdbdgc' => $xsh_gmdzdbdgc,
            'xsh_dmdzdbdgc' => $xsh_dmdzdbdgc,
            'xsh_baijiesu_6' => $xsh_baijiesu_6,
            'xcg_xuehongdanbai' => $xcg_xuehongdanbai,
            'xcg_baixibao' => $xcg_baixibao,
            'xcg_xibaojishu' => $xcg_xibaojishu,
            'xcg_linba_xibaojishu' => $xcg_linba_xibaojishu,
            'xcg_hongxibao' => $xcg_hongxibao,
            'xcg_xuexiaoban' => $xcg_xuexiaoban
        ];

        //判断数据是否存在
        $info = $this->shiyanshiModel->where('pid', $pid)->with(['huanzhe' => function ($query) {
            $query->field('id,hospital_id,keshi,name,gender,birthday,created');
        }])->findOrEmpty()->toArray();
        if(empty($info)){
            $res = $this->shiyanshiModel->save($data);
        }else{
            $res = $this->shiyanshiModel->save($data,['pid' => $info['pid']]);
        }

        if ($res) {
            $resBack = jsonRes($res);
        } else {
            $resBack = jsonRes($res, '更新失败', 99);
        }
        return $resBack;
    }
}